package sorting

import "gitee.com/guuzaa/sorting/constraints"

func CocktailSort[T constraints.Ordered](arr []T) []T {
	if len(arr) < 2 {
		return arr
	}

	flag := true
	n := len(arr)

	for i := 0; i < (n<<1) && flag; i++ {
		flag = false

		for j := i; j < n-i-1; j++ {
			if arr[j] > arr[j+1] {
				arr[j], arr[j+1] = arr[j+1], arr[j]
				flag = true
			}
		}

		for j := n - i - 2; j >= i+1; j-- {
			if arr[j] < arr[j-1] {
				arr[j], arr[j-1] = arr[j-1], arr[j]
				flag = true
			}
		}
	}

	return arr
}
